home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 22 / CU Amiga Magazine's Super CD-ROM 22 (1998)(EMAP Images)(GB)[!][issue 1998-05].iso / PowerPC / Programming / PPCsiod / sources / mathpred.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-22  |  6.2 KB  |  237 lines

  1. /* Scheme In One Define.
  2.  
  3. The garbage collector, the name and other parts of this program are
  4.  
  5.  *                     COPYRIGHT (c) 1989 BY                              *
  6.  *      PARADIGM ASSOCIATES INCORPORATED, CAMBRIDGE, MASSACHUSETTS.       *
  7.  
  8. Conversion  to  full scheme standard, characters, vectors, ports, complex &
  9. rational numbers, and other major enhancments by
  10.  
  11.  *      Scaglione Ermanno, v. Pirinoli 16 IMPERIA P.M. 18100 ITALY        * 
  12.  
  13. Permission  to use, copy, modify, distribute and sell this software and its
  14. documentation  for  any purpose and without fee is hereby granted, provided
  15. that  the  above  copyright  notice appear in all copies and that both that
  16. copyright   notice   and   this  permission  notice  appear  in  supporting
  17. documentation,  and that the name of Paradigm Associates Inc not be used in
  18. advertising or publicity pertaining to distribution of the software without
  19. specific, written prior permission.
  20.  
  21. PARADIGM  DISCLAIMS  ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  22. ALL  IMPLIED  WARRANTIES  OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  23. PARADIGM  BE  LIABLE  FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  24. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  25. IN  AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  26. OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  27.  
  28. */
  29.  
  30. #include <stdio.h>
  31. #include <string.h>
  32. #include <ctype.h>
  33. #include <setjmp.h>
  34. #include <signal.h>
  35. #include <math.h>
  36. #include <limits.h>
  37.  
  38. #include "siod.h"
  39.  
  40.  
  41. LISP negative(LISP x)
  42. {if NNUMBERP(x) err("negative?",x,ERR_GEN_ARG | ERR_NNUM);
  43.  return EQ(truth,lessp(x,flocons(0.))) ? truth : NIL;}
  44.  
  45. LISP positive(LISP x)
  46. {if NNUMBERP(x) err("positive?",x,ERR_GEN_ARG | ERR_NNUM);
  47.  return EQ(truth,greaterp(x,flocons(0.))) ? truth : NIL;}
  48.  
  49. LISP zerop(LISP x)
  50. {if NNUMBERP(x) err("zero?",x,ERR_GEN_ARG | ERR_NNUM);
  51.  return EQ(truth,uguale(x,flocons(0.))) ? truth : NIL;}
  52.  
  53. LISP odd(LISP x)
  54. {double tmp;
  55.  x = tofloat(x);
  56.  if (NFLONUMP(x)||(modf(FLONM(x),&tmp)!=0.)) 
  57.      err("odd?",x,ERR_GEN_ARG | ERR_NINT);
  58.  if(fmod(FLONM(x),2.)==0.)
  59.    return(NIL);
  60.  return(truth);}
  61.  
  62. LISP even(LISP x)
  63. {double tmp;
  64.  x = tofloat(x);
  65.  if (NFLONUMP(x)||(modf(FLONM(x),&tmp)!=0.)) 
  66.    err("even?",x,ERR_GEN_ARG | ERR_NINT); 
  67.  if(fmod(FLONM(x),2.)==0.)
  68.    return(truth);
  69.  return(NIL);}
  70.  
  71.  
  72. LISP lessp(LISP x,LISP y)
  73. {
  74.  if NNUMBERP(x) err("<",x,ERR_FIRST | ERR_NNUM);
  75.  if NNUMBERP(y) err("<",y,ERR_SECOND | ERR_NNUM);
  76.  if(NCOMPNUMP(x) && NCOMPNUMP(y))
  77.    {y=tofloat(y);
  78.     x=tofloat(x);
  79.     if(FLONM(x)<FLONM(y)) return(truth);}
  80.  else
  81.    {y=tocomplex(y);
  82.     x=tocomplex(x);
  83.     if(COMPRE(x)<COMPRE(y)) return(truth);
  84.     else if((COMPRE(x) == COMPRE(y)) && (COMPIM(x)<COMPIM(y)))
  85.         return(truth);}
  86.  return(NIL);}
  87.  
  88. LISP greaterp(LISP x,LISP y)
  89. {
  90.  if NNUMBERP(x) err(">",x,ERR_FIRST | ERR_NNUM);
  91.  if NNUMBERP(y) err(">",y,ERR_SECOND | ERR_NNUM);
  92.  if(NCOMPNUMP(x) && NCOMPNUMP(y))
  93.    {y=tofloat(y);
  94.     x=tofloat(x);
  95.     if(FLONM(x)>FLONM(y)) return(truth);}
  96.  else
  97.    {y=tocomplex(y);
  98.     x=tocomplex(x);
  99.     if(COMPRE(x)>COMPRE(y)) return(truth);
  100.     else if((COMPRE(x) == COMPRE(y)) && (COMPIM(x)>COMPIM(y)))
  101.         return(truth);}
  102.  return(NIL);}
  103.  
  104. LISP greatereqp(LISP x,LISP y)
  105. {
  106.  if NNUMBERP(x) err(">=",x,ERR_FIRST | ERR_NNUM);
  107.  if NNUMBERP(y) err(">=",y,ERR_SECOND | ERR_NNUM);
  108.  if(NCOMPNUMP(x) && NCOMPNUMP(y))
  109.    {y=tofloat(y);
  110.     x=tofloat(x);
  111.     if(FLONM(x)>=FLONM(y)) return(truth);}
  112.  else
  113.    {y=tocomplex(y);
  114.     x=tocomplex(x);
  115.     if(COMPRE(x)>=COMPRE(y)) return(truth);
  116.     else if((COMPRE(x) == COMPRE(y)) && (COMPIM(x)>=COMPIM(y)))
  117.         return(truth);}
  118.  return(NIL);}
  119.  
  120. LISP lesseqp(LISP x,LISP y)
  121. {
  122.  if NNUMBERP(x) err("<=",x,ERR_FIRST | ERR_NNUM);
  123.  if NNUMBERP(y) err("<=",y,ERR_SECOND | ERR_NNUM);
  124.  if(NCOMPNUMP(x) && NCOMPNUMP(y))
  125.    {y=tofloat(y);
  126.     x=tofloat(x);
  127.     if(FLONM(x)<=FLONM(y)) return(truth);}
  128.  else
  129.    {y=tocomplex(y);
  130.     x=tocomplex(x);
  131.     if(COMPRE(x)<=COMPRE(y)) return(truth);
  132.     else if((COMPRE(x) == COMPRE(y)) && (COMPIM(x)<=COMPIM(y)))
  133.         return(truth);}
  134.  return(NIL);}
  135.  
  136. LISP uguale(LISP x,LISP y)
  137. {
  138.  if NNUMBERP(x) err("=",x,ERR_FIRST | ERR_NNUM);
  139.  if NNUMBERP(y) err("=",y,ERR_SECOND | ERR_NNUM);
  140.  if(NCOMPNUMP(x) && NCOMPNUMP(y))
  141.    {y=tofloat(y);
  142.     x=tofloat(x);
  143.     if(FLONM(x)==FLONM(y)) return(truth);}
  144.  else
  145.    {y=tocomplex(y);
  146.     x=tocomplex(x);
  147.     if((COMPRE(x) == COMPRE(y)) && (COMPIM(x)==COMPIM(y)))
  148.         return(truth);}
  149.  return(NIL);}
  150.  
  151. LISP diverso(LISP x,LISP y)
  152. {
  153.  if NNUMBERP(x) err("<>",x,ERR_FIRST | ERR_NNUM);
  154.  if NNUMBERP(y) err("<>",y,ERR_SECOND | ERR_NNUM);
  155.  if(NCOMPNUMP(x) && NCOMPNUMP(y))
  156.    {y=tofloat(y);
  157.     x=tofloat(x);
  158.     if(FLONM(x)!=FLONM(y)) return(truth);}
  159.  else
  160.    {y=tocomplex(y);
  161.     x=tocomplex(x);
  162.     if((COMPRE(x) != COMPRE(y)) || (COMPIM(x)!=COMPIM(y)))
  163.         return(truth);}
  164.  return(NIL);}
  165.  
  166. LISP eq(LISP x,LISP y)
  167. {if(EQ(x,y)) return(truth);
  168.  return(NIL);}
  169.  
  170. LISP eql(LISP x,LISP y)
  171. {if(EQ(x,y)) 
  172.     return(truth);
  173.  if(NUMBERP(x) && NUMBERP(y))
  174.     return(uguale(x,y));
  175.  if(STRINGP(x) && STRINGP(y))
  176.    if(strcmp(SNAME(x),SNAME(y))==0)
  177.        return(truth);
  178.  if(CHARP(x) && CHARP(y))
  179.    if(CHARV(x)==CHARV(y))
  180.        return(truth);
  181.  return(NIL);}
  182.  
  183. LISP equal(LISP x,LISP y)
  184. {long size1,size2,i;
  185.  if(VECTORP(x) && VECTORP(y))
  186.   {size1 = VECSIZE(x);
  187.    size2 = VECSIZE(y);
  188.    if(size1!=size2)
  189.      return(NIL);
  190.    for(i=0;i<size1;i++)
  191.      if(equal(VECTOR(x)[i],VECTOR(y)[i])==NIL)
  192.        return(NIL);
  193.    return(truth);}
  194.  if(CONSP(x) && CONSP(y))
  195.    if((equal(CAR(x),CAR(y))==truth)&&(equal(CDR(x),CDR(y))==truth))
  196.       return(truth);
  197.  return(eql(x,y));}
  198.  
  199. LISP integerp(LISP x)
  200. {double tmp;
  201.  if (INTNUMP(x)|| (FLONUMP(x)&&(modf(FLONM(x),&tmp)==0.))) 
  202.     return(truth); else return(NIL);}
  203.  
  204. LISP rationalp(LISP x)
  205. {if RATNUMP(x) return(truth); else return(NIL);}
  206.  
  207. LISP floatp(LISP x)
  208. {if FLONUMP(x) return(truth); else return(NIL);}
  209.  
  210. LISP complexp(LISP x)
  211. {if COMPNUMP(x) return(truth); else return(NIL);}
  212.  
  213. int nump(LISP x)
  214. {switch(TYPE(x))
  215.  {case tc_intnum:
  216.   case tc_ratnum:
  217.   case tc_flonum:
  218.   case tc_compnum:
  219.     return(1);
  220.   default:
  221.     return(0);}}
  222.  
  223. LISP numberp(LISP x)
  224. {if(NUMBERP(x))
  225.    return(truth);
  226.  else
  227.    return(NIL);}
  228.  
  229. LISP realp(LISP x)
  230. {switch(TYPE(x))
  231.  {case tc_intnum:
  232.   case tc_ratnum:
  233.   case tc_flonum:
  234.     return(truth);
  235.   default:
  236.     return(NIL);}}
  237.